(function () {
  if (!window.StyleFix) return;
  if (hasxmldatauri()) return;
  function hasxmldatauri() {
    const img = new Image();
    datauri =
      'data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg"></svg>';
    img.src = datauri;
    return img.width > 0 && img.height > 0;
  }
  StyleFix.regist(function (css) {
    return css.replace(
      RegExp(/url\(([\'\"])data:image\/svg\+xml,(.*)\1\)/gi),
      function ($0, $1, svg) {
        return "url('data:image/svg+xml," + encodeURI(svg) + "')";
      }
    );
  });
})();

/**
 * 转义HTML标签的方法
 * @param {unknown} str 需要转义的HTML字符串
 * @returns 转义后的字符串
 */
function funEncodeHtml(str) {
  if (typeof str !== "string") {
    return "";
  }
  return str.replace(/<|&|>/g, function (matches) {
    return {
      "<": "&lt;",
      ">": "&gt;",
      "&": "&amp;",
    }[matches];
  });
}
/**
 * 反转义HTML标签的方法
 * @param {unknown} str 需要反转义的字符串
 * @returns 反转义后的字符串
 */
function funDecodeHtml(str) {
  if (typeof str !== "string") {
    return "";
  }
  return str.replace(/&lt;|&gt;|&amp;/g, function (matches) {
    return {
      "&lt;": "<",
      "&gt;": ">",
      "&amp;": "&",
    }[matches];
  });
}

function funDownload(content, filename) {
  const eleLink = document.createElement("a");
  eleLink.download = filename;
  eleLink.style.display = "none";
  // 字符内容转变成blob地址
  const blob = new Blob([content]);
  eleLink.href = URL.createObjectURL(blob);
  // 触发点击
  document.body.appendChild(eleLink);
  eleLink.click();
  // 然后移除
  document.body.removeChild(eleLink);
}
function funDownloadToBase64(domImg, filename) {
  // 创建隐藏的可下载链接
  const eleLink = document.createElement("a");
  eleLink.download = filename;
  eleLink.style.display = "none";
  // 图片转base64地址
  const canvas = document.createElement("canvas");
  const context = canvas.getContext("2d");
  const _width = domImg.naturalWidth,
    _height = domImg.naturalHeight;
  context.drawImage(domImg, 0, 0);
  // 如果是PNG图片，则canvas.toDataURL('image/png')
  eleLink.href = canvas.toDataURL("image/jpeg");
  // 触发点击
  document.body.appendChild(eleLink);
  eleLink.click();
  // 然后移除
  document.body.removeChild(eleLink);
}

/**
 * 将svg文本转成data:image/svg+xml格式的url
 * @param {string} str svg文本
 * @returns
 * @example encodeSvg('<svg xmlns="http://www.w3.org/2000/svg"></svg>')
 */
function encodeSvg(str) {
  return (
    "data:image/svg+xml," +
    str
      .replace(/"/g, "'")
      .replace(/%/g, "%25")
      .replace(/#/g, "%23")
      .replace(/{/g, "%7B")
      .replace(/}/g, "%7D")
      .replace(/</g, "%3C")
      .replace(/>/g, "%3E")
  );
}

/**
 * 获取元素样式变量值
 * @param {string} ele 元素选择器
 * @param {string} name 样式变量名称
 * @returns
 */
function getEleStyleVariableValue(ele, name) {
  if (name.indexOf("--") !== 0) {
    throw new Error("样式变量名称必须以--开头！");
  }
  let el;
  if (!ele instanceof HTMLElement) {
    el = document.querySelector(ele);
  } else {
    el = ele;
  }
  if (!el) {
    throw new Error("未找到指定元素！");
  }
  const style = window.getComputedStyle(el);
  return style.getPropertyValue(name);
}
